<!-- 发布求助 -->
<template>
  <div>
    <van-nav-bar
      title="寻求帮助"
      left-text="返回"
      @click-left="onClickLeft"
      left-arrow
      style="margin-bottom: 6px"
    />
    <van-form @submit="onSubmit" style="padding-right: 6px">
      <van-cell-group>
        <van-field v-model="value" label="求助标题" placeholder="请输入标题" />
        <van-field
          v-model="message"
          rows="1"
          autosize
          label="求助原因"
          type="textarea"
          placeholder="请输入求助内容"
        />
        <van-field
          readonly
          clickable
          name="datetimePicker"
          :value="timeValue"
          label="开始时间"
          placeholder="点击选择时间"
          @click="showPicker = true"
        />
        <van-popup
          v-model="showPicker"
          position="bottom"
          style="border-radius: 15px 15px 0 0"
        >
          <van-datetime-picker
            v-model="currentDate"
            type="datetime"
            title="选择完整时间"
            :min-date="minDate"
            :max-date="maxDate"
            @confirm="onConfirm"
            @cancel="showPicker = false"
          />
        </van-popup>

        <van-field
          readonly
          clickable
          name="datetimePicker"
          :value="timeValueEnd"
          label="结束时间"
          placeholder="点击选择时间"
          @click="showPickerEnd = true"
        />
        <van-popup
          v-model="showPickerEnd"
          position="bottom"
          style="border-radius: 15px 15px 0 0"
        >
          <van-datetime-picker
            v-model="currentDate"
            type="datetime"
            title="选择完整时间"
            :min-date="minDate"
            :max-date="maxDate"
            @confirm="onConfirmEnd"
            @cancel="showPickerEnd = false"
          />
        </van-popup>

        <van-field name="radio" label="所属区域">
          <template #input>
            <van-radio-group v-model="radio" v-for="partition in actions" direction="horizontal">
              <van-radio :name="partition.id" @click="click(partition)">{{partition.name}}</van-radio>
            </van-radio-group>
          </template>
        </van-field>

        <van-field
            v-model="location"
            rows="1"
            autosize
            label="求助地点"
            type="textarea"
            placeholder="请填写求助地点"
        />
        <van-field
            v-model="school"
            autosize
            label="学校"
            type="textarea"
            placeholder="请填写学校"
        />
        <van-field
          readonly
          clickable
          name="picker"
          :value="selectValue"
          label="所属类别"
          placeholder="点击选择类别"
          @click="showPickerSelect = true"
        />
        <van-popup
          v-model="showPickerSelect"
          position="bottom"
          style="border-radius: 15px 15px 0 0"
        >
          <van-picker
            show-toolbar
            :columns="columns"
            @confirm="onConfirmSelect"
            @cancel="showPickerSelect = false"
          />
        </van-popup>

        <van-field name="rate" label="难度">
          <template #input>
            <van-rate v-model="hardValue" @change="onChange" />
          </template>
        </van-field>

        <van-field
            v-model="score"
            rows="1"
            autosize
            label="积分报酬"
            type="textarea"
            placeholder="请输入具体数额"
        />
        <van-field name="uploader" label="位置图片">
          <template #input>
            <van-uploader v-model="uploader" />
          </template>
        </van-field>

        <!-- 需要人数 -->
        <van-field
          v-model="num"
          label="需要人数"
          placeholder="输入需要人数"
          type="tel"
          maxlength="2"
          show-word-limit
        />

        <van-field
            v-model="contact"
            rows="1"
            autosize
            label="联系方式"
            type="textarea"
            placeholder="请填写联系方式"
        />
      </van-cell-group>

      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" @click=""
          >发布</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { Toast } from "vant";

export default {
  name: "PostHelp",
  data() {
    return {
      value: "",
      message: "",
      timeValue: "",
      timeValueEnd: "",
      minDate: new Date(2022, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      showPicker: false,
      showPickerEnd: false,
      radio: 1000,
      selectValue: "",
      exchange: 1000,
      columns: [
          "学习区",
          "失物招领",
          "校园生活",
          "兼职区",
          "校园情区"
      ],
      actions: [{id: 1000, name: "本校区" }, {id: 2000, name: "全国区" }],
      navigate_country: [
        "学习区",
        "生活区",
        "情感区"
      ],
      navigate_school: [
        "学习区",
        "失物招领",
        "校园生活",
        "兼职区",
        "校园情区"
      ],
      showPickerSelect: false,
      hardValue: "",
      uploader: [],
      num: null,
      location: '',
      contact: '',
      userId: null,
      score: null,
      school: null,
    };
  },
  methods: {
    onClickLeft() {
      Toast("返回");
      history.go(-1)
    },
    onSubmit(values) {
      console.log("submit", values);
      const url = 'http://localhost:10005/cooperation/addNewCooperation'
      const dat = {
        address: this.location,
        applyLimit: this.num,
        userId: 1,
        applyType: 0,
        startTime: this.timeValue,
        endTime: this.timeValueEnd,
        helpContent: this.message,
        helpTitle: this.value,
        moduleId: this.moduleId,
        rewardType: '积分',
        rewardValue: this.score,
        school: this.school,
        applyBelimit: 0,
        difficulty: this.hardValue,
        helpState: 0
      }
      this.axios.post(url,dat).then(response =>{
        console.log(dat)
        if(response.data.state==200){
          console.log(response.data)
          Toast("发布成功")
          this.$router.push('/homeview/help/helppage')
        }
        else {
          console.log('注册失败')
          Toast("注册失败")
        }
      })
    },
    formatDate(timeValue) {
      return `${timeValue.getFullYear()}/${
        timeValue.getMonth() + 1
      }/${timeValue.getDate()} ${timeValue.getHours()}:${timeValue.getMinutes()}`;
    },
    onConfirm(timeValue) {
      this.showPicker = false;
      this.timeValue = this.$d(timeValue).format("YYYY-MM-DD HH:mm");
    },
    onConfirmEnd(timeValueEnd) {
      this.showPickerEnd = false;
      this.timeValueEnd = this.$d(timeValueEnd).format("YYYY-MM-DD HH:mm");
    },
    onConfirmSelect(selectValue) {
      if(this.exchange==1000){
        if(selectValue=="学习区"){
          this.moduleId = 1001
        }else if(selectValue=="失物招领"){
          this.moduleId = 1002
        }else if(selectValue=="校园生活"){
          this.moduleId = 1003
        }else if(selectValue=="兼职区"){
          this.moduleId = 1004
        }else{
          console.log("校园情区")
          this.moduleId = 1005
        }
      }else {
        if(selectValue=="学习区"){
          this.moduleId = 2001
        }else if(selectValue=="生活区"){
          this.moduleId = 2002
        }else{
          console.log("情感区")
          this.moduleId = 2003
        }
      }
      this.showPickerSelect = false
      this.selectValue = selectValue
    },
    onChange(hardValue) {
      Toast("当前值：" + hardValue);
    },
    click(event){
      console.log(event)
      if(event.id==1000){
          this.columns = this.navigate_school
          this.exchange = event.id
        console.log(event.name)
      }else{
        this.columns = this.navigate_country
        this.exchange = event.id
        console.log(event.name)
      }
    }
  },
};
</script>

<style scoped>
</style>